var vm = new Vue({
  el:'#app',
  data:{
    mode:'arti',//arti 人工发货, auto 自动发货
    upload_type:'sing',//sing,单张/批量截图,many,批量卡密文本
    is_read:false,//是否阅读了协议
    pay_money:0,
    kami_text:null,
    upload_type:'sing',//上传卡密的方式
    upload_list:[
      {
          pic:'',//卡密图片id
          sn:'',//卡密图片
          src:'',//卡密图片
      }
    ],    
    id:null,//订单id       
    pwd:null,
  },
  methods: {
    acc: function (arg1, arg2) {
      var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
      try { m += s1.split(".")[1].length } catch (e) { }
      try { m += s2.split(".")[1].length } catch (e) { }
      return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
    },
    // 切换发货方式
    changeMode:function(mode){
      this.mode = mode;
    },
    //切换上传方式
    change_upload_type:function(type){
      this.upload_type = type;
    },
    // 切换是否阅读协议
    change_is_read:function(){
      this.is_read = !this.is_read;
    },
    add_pic:function(){
      this.upload_list.push({
          pic:'',//卡密图片id
          sn:'',//卡密图片
          src:'',//卡密图片地址
      })
    },
     // 切换上传卡密方式
     change_upload_type:function(type){
      this.upload_type = type;
    },
    // 删除对应的这个卡密
    del_card:function(index){
      console.log(index);
      this.upload_list.splice(index,1)
  },
    //关闭支付
     close_pay:function() {
      // window.location.href = '/view/publish/fbcscg.html?&activityId=' +vm.id;
      window.location.href = "/view/my/fabu_chushou.html";

      $('.reward').hide()
    },
    click_upload:function(e){
      $(e.currentTarget).siblings('.upload_pic').trigger('click');
    },
    // 上传图片
    upload_pic:function(type,e,item,index){
      var that = this;
      var inputDom = e.currentTarget;
      var files = inputDom.files;
      var len;
      if(type == 'dan'){
          len = 1;
          // 单张上传
      }else{
          //批量
          len = files.length;
      }
      for(var i=0; i<len; i++){
          var formdata = new FormData();
          formdata.append('file',files[i]);
          formdata.append('dir','images');
          $.ajax({
              url: app.webUrl+'/index/plugin/execute/_plugin/WxSdk/_controller/Api/_action/file', /*接口域名地址*/
              type:'post',
              data: formdata,
              contentType: false,
              processData: false,
              success:function(ret){
                  var data = JSON.parse(ret);
                  if(data.code == '1' && data.qrtxt){
                      var obj = {
                          pic:data.id,
                          sn:data.qrtxt,
                          src:data.url
                      };
                      // 添加到upload_list
                      if(type == 'dan'){
                          // 单张模式
                          item.pic = data.id;
                          item.sn = data.qrtxt;
                          item.src = data.url;
                      }else{
                          // 批量模式
                          that.upload_list.unshift(obj);
                      }
                  }else{
                      vant.Toast('请上传清晰的二维码或者条形码图片')
                  }
              }
          })
      }

  },
    //确认发布
  //   qrfb:function(){
      
  //     var that = this;
  //     vant.Dialog.confirm({
  //         message: '确认发布吗?',
  //     }).then(function () {
  //        that.qrfb2();
  //     }).catch(function () {
  //         // return;
  //     });
  // },
    qrfb:function(){
      if (this.mode=="auto" && this.kami_text == ""&& this.upload_type == "many") {
        vant.Toast({ message: "请上传卡密信息");
        return
      }
      if (this.mode=="auto" && this.upload_list.pic== ""&& this.upload_type == "sing") {
        vant.Toast({ message: "请上传卡密信息");
        return
      }
      if (this.is_read!=true) {
        vant.Toast({ message: "请同意用户协议", position: "bottom"});
        return
      }
      var that = this;
            var data_sn = that.upload_list.map(function(item){
                return {
                    pic:item.pic,
                    sn:item.sn
                }
            }).filter(function(item){
                if(item.pic != '' && item.sn != ''){
                    return true;
                }else{
                    return false;
                }
            });
            var value = null;
            if(that.upload_type == 'sing'){
                // 单张/批量马图
                value = {
                    id:that.now_item_id,
                    sn:JSON.stringify(data_sn)
                }
            }else{
                //卡密文本
                var marr = that.kami_text.split(/\n/);
                var arr = marr.filter(function(item){
                    return item != "";
                }).map(function(item){
                    return {
                        sn:item
                    }
                });
                value = {
                    id:that.now_item_id,
                    sn:JSON.stringify(arr)
                }
            };
            console.log(value.sn);
            
      var set_cookie=JSON.parse($.cookie("set"));
      var data={}
      data.name=set_cookie.name;
      data.type=set_cookie.type;
      data.desc=set_cookie.desc;
      data.amount=set_cookie.amount;
      data.mark_amount=set_cookie.amount_sc;
      data.num=set_cookie.num;
      data.take_time=set_cookie.take_time;
      data.invalid_time=set_cookie.invalid_time;
      data.goods_type=1;
      data.ensure=0;
      if(that.mode=="auto"){
      data.ensure=1;
      data.cards=value.sn;
      }
      data.name=set_cookie.name;
      document.getElementById("fabu").disabled = true;
      $.ajax({
        url: app.webUrl+'/view/api_card_sell/publish_sell', /*接口域名地址*/
        type:'post',
        data: data,
        // contentType: false,
        // processData: false,
        success:function(ret){
      document.getElementById("fabu").disabled = false;
      if(ret.code == '1'){

            $.removeCookie('set');
            $.removeCookie('set_leixing');
            window.localStorage.removeItem("set_leixing");
              // vant.Toast('发布成功')
              vm.id=ret.data.id
              $('.reward').css('display', 'block')
            }else{
              vant.Toast(ret.msg)

            }
        }
    })
      
    },
    ljzf:function(){
      this.pwd = $("input[name = 'password']").val();
      if (this.pwd.length != 6) {
        vant.Toast({ message: "请输入六位数的支付密码");
        return
      }
      document.getElementById("zhifu").disabled = true;
      $.ajax({
        url: app.webUrl + "/view/api_card_sell/publish_pay",
        type: "POST",
        data: {
          tid:vm.id,
          pwd:app.doublemd5(this.pwd)
        },
        success: function (ret) {
          document.getElementById("zhifu").disabled = false;
          console.log(ret);
          if (ret.code == '1') {
            $.removeCookie('set');
            $.removeCookie('set_leixing');
            window.localStorage.removeItem("set_leixing");
            // vant.Toast({ message: "支付成功");
            // setTimeout(function () {
              window.location.href = '/view/publish/fbcscg.html?&activityId=' +vm.id;
            // }, 100)
          }else{
            vant.Toast({ message: ret.msg);
          }
        },
      });
    },
  },
})
$(".password-div input").on("input",function(e){ //标签为password-div下的input添加oninput事件
  var number = 6;   //定义输入最大值
  var pw = $("input[name = 'password']").val(); //定义pw为name是password的input框的输入值
  var list = $(".password-div ul li");  //定义list是li
  for(var i = 0; i < number ; i++){    //for循环遍历将·放入li标签
    if(pw[i]){
      $(list[i]).text("·");
    }else{
      $(list[i]).text("");
    };
  };
});


// 点击空白清空密码
$(".password-div ul").click(function(){      
  $("input[name = 'password']").val("");
  $("#password").focus();
  $(".password-div ul li").text("");
});



// 点击发货方式变颜色
// $('.ship').click(function() {
//   $('.ship div').toggleClass('back')

// })

// $('.ship_auto').click(function() {
//   $('.automatic').css('display', 'block')
// })

// $('.ship_one').click(function() {
//   $('.automatic').css('display', 'none')
// })


// 点击图片卡密或文字卡密
$('.auto_num span').click(function() {
  $('.auto_num span').removeClass('target')
  $(this).toggleClass('target')
})

$('.auto_num .dan').click(function() {
  $('.many').css('display', 'none')
  $('.sing').css('display', 'block')
})
$('.auto_num .duo').click(function() {
  $('.many').css('display', 'block')
  $('.sing').css('display', 'none')
})

// function qrfb() {
//   $('.reward').css('display', 'block')
// }

function ljzf() {
  window.location.href='/view/publish/fbcscg.html'
}

// 是否认真读了xxx 
// function changeXY(){
//   $('.xieyi .gou').toggleClass('gou_w')
// }

// 关闭支付框
function close_pay() {
  window.location.href = '/view/publish/fbcscg.html?&activityId=' +this.vm.id;
  $('.reward').hide()
}

